package com.site.service.impl.dining;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.site.dao.dining.BanquetHallDao;
import com.site.dto.dining.BanquetHallBizBean;
import com.site.dto.dining.DishesBizBean;
import com.site.model.dining.BanquetHallInfo;
import com.site.model.dining.DishesInfo;
import com.site.model.dining.RestaurantInfo;
import com.site.service.dining.BanquetHallService;
import com.site.util.PropertyUtils;
import com.site.util.base.businessbean.FileBean;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.exception.ApplicationException;
import com.site.util.exception.SystemException;
import com.site.util.extremetable.QueryPage;

/**
 * 宴会厅Service
 * @author win7
 *
 */
@Service
public class BanquetHallServiceImpl implements BanquetHallService {

	private Log log=LogFactory.getLog(this.getClass());
	
	private BanquetHallDao banquetHallDao;
	
	@Resource
	public void setBanquetHallDao(BanquetHallDao banquetHallDao) {
		this.banquetHallDao = banquetHallDao;
	}

	@Override
	public Object execute(Object obj, String command) throws SystemException,
			ApplicationException {
		// 删除
		if(COMMAND_DELETE.equals(command)){
			delete((Map<String, Object>)obj);
		// 增加
		} else if(COMMAND_ADD.equals(command)){
			add((Map<String, Object>)obj);
		// 修改
		} else if(COMMAND_UPDATE.equals(command)){
			eidt((Map<String, Object>)obj);
		}
		return null;
	}


	/**
	 * 修改
	 * @param obj
	 */
	private void eidt(Map<String, Object> map) {
		if(null != map && !map.isEmpty()){
			BanquetHallBizBean banquetHallBizBean = (BanquetHallBizBean)map.get("banquetHallBizBean");
			BanquetHallInfo banquetHallInfo = banquetHallDao.getEntityById(BanquetHallInfo.class, banquetHallBizBean.getId());
			// 旧图片地址
			String oldPath = banquetHallInfo.getImagePath();
			FileUploadHandle fileUploadHandle = (FileUploadHandle)map.get("fileUploadHandle");
			if(null != fileUploadHandle && fileUploadHandle.getFiles() != null && !fileUploadHandle.getFiles().isEmpty()){
				// 如果有就图片，就删除相应的图片
				if(null != banquetHallInfo && !StringUtils.isEmpty(banquetHallInfo.getImagePath())){
					String path = fileUploadHandle.getRequest().getSession().getServletContext().getRealPath("/restaurant");
					File file = new File(path+banquetHallInfo.getImagePath());
					if(null != file && file.exists()){
						file.delete();
						log.info("餐厅宴席厅图   "+banquetHallInfo.getImagePath()+" 正在删除…");
					}
				}
				
				// 上传路径[宴席图]目录
				String rno="";
				if(null != oldPath && !StringUtils.isEmpty(oldPath)){
					rno = oldPath.substring(1,7);
				} else {
					RestaurantInfo restaurantInfo = banquetHallDao.getEntityById(RestaurantInfo.class, banquetHallBizBean.getRid());
					if(null != restaurantInfo && !StringUtils.isEmpty(restaurantInfo.getRno())){
						rno = restaurantInfo.getRno();
					}
				}
				String mainPath = "/restaurant/"+rno+"/BanquetHall";
				fileUploadHandle.setUploadDir(RestaurantServiceImpl.getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							banquetHallBizBean.setImagePath("/"+rno+"/BanquetHall/"+fileBean.getNewName());
							log.info("餐厅宴席厅图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
			PropertyUtils.copyNotNullProperties(banquetHallInfo, banquetHallBizBean);
			banquetHallDao.update(banquetHallInfo);
		}
	}

	/**
	 * 增加
	 * @param obj
	 */
	private void add(Map<String, Object> map) {
		if(map != null && !map.isEmpty()){
			BanquetHallBizBean banquetHallBizBean = (BanquetHallBizBean)map.get("banquetHallBizBean");
			RestaurantInfo restaurantInfo = banquetHallDao.getEntityById(RestaurantInfo.class, banquetHallBizBean.getRid());
			BanquetHallInfo banquetHallInfo = new BanquetHallInfo();
			PropertyUtils.copyProperties(banquetHallInfo, banquetHallBizBean);
			banquetHallDao.save(banquetHallInfo);
			// 增加宴席图片
			FileUploadHandle fileUploadHandle = (FileUploadHandle)map.get("fileUploadHandle");
			if(null != fileUploadHandle && fileUploadHandle.getFiles() != null && !fileUploadHandle.getFiles().isEmpty()){
				// 上传路径[宴席图]目录
				String mainPath = "/restaurant/"+restaurantInfo.getRno()+"/BanquetHall";
				fileUploadHandle.setUploadDir(RestaurantServiceImpl.getUploadPath(fileUploadHandle.getRequest(), mainPath));
				List<FileBean> fileBeans = fileUploadHandle.handleUploadFile();
				
				if(null != fileBeans && !fileBeans.isEmpty()){
					for (FileBean fileBean : fileBeans) {
						if(null != fileBean && !StringUtils.isEmpty(fileBean.getNewName())){
							banquetHallInfo.setImagePath("/"+restaurantInfo.getRno()+"/BanquetHall/"+fileBean.getNewName());
							log.info("餐厅宴席厅图  "+fileBean.getNewName()+" 正在上传…");
						}
					}
				}
			}
		}
	}

	/**
	 * 删除
	 * @param obj
	 */
	private void delete(Map<String, Object> map) {
		if(null != map && !map.isEmpty()){
			BanquetHallBizBean banquetHallBizBean  = (BanquetHallBizBean)map.get("banquetHallBizBean");
			BanquetHallInfo banquetHallInfo = banquetHallDao.getEntityById(BanquetHallInfo.class, banquetHallBizBean.getId());
			// 删除相应的图片
			if(null != banquetHallInfo){
				HttpServletRequest request = (HttpServletRequest)map.get("request");
				String path = request.getSession().getServletContext().getRealPath("/restaurant");
				File file = new File(path+banquetHallInfo.getImagePath());
				if(null != file && file.exists()){
					file.delete();
				}
				log.info("餐厅宴席厅图   "+banquetHallInfo.getImagePath()+" 正在删除…");
				banquetHallDao.delete(BanquetHallInfo.class, banquetHallInfo.getId());
			}
		}
	}

	@Override
	public List<BanquetHallBizBean> getBanquetHalls(QueryPage queryPage,
			BanquetHallBizBean banquetHallBizBean) {
		List<BanquetHallInfo> banquetHallInfos = banquetHallDao.getBanquetHalls(queryPage, banquetHallBizBean);
		if(null != banquetHallInfos && !banquetHallInfos.isEmpty()){
			List<BanquetHallBizBean> banquetHallBizBeans = new ArrayList<BanquetHallBizBean>();
			for (BanquetHallInfo banquetHallInfo : banquetHallInfos) {
				BanquetHallBizBean banquetHallBizBean2 = new BanquetHallBizBean();
				PropertyUtils.copyProperties(banquetHallBizBean2, banquetHallInfo);
				banquetHallBizBeans.add(banquetHallBizBean2);
			}
			if(null != banquetHallBizBeans && !banquetHallBizBeans.isEmpty()){
				return banquetHallBizBeans;
			}
		}
		return null;
	}

	@Override
	public BanquetHallBizBean getEntityById(Long id) {
		BanquetHallInfo banquetHallInfo = banquetHallDao.getEntityById(BanquetHallInfo.class, id);
		if(null != banquetHallInfo){
			BanquetHallBizBean banquetHallBizBean = new BanquetHallBizBean();
			PropertyUtils.copyProperties(banquetHallBizBean, banquetHallInfo);
			return banquetHallBizBean;
		}
		return null;
	}
}
